<template>
	<view class="height100">
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{ current: tabCurrentIndex === index }" @click="tabClick(index)">{{ item.text }}</view>
		</view>

		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem, tabIndex) in navList" :key="tabIndex">
				<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData">
					<!-- 空白页 -->
					<empty v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty>

					<!-- 订单列表 -->
					<view v-for="(item, index) in tabItem.orderList" :key="index" class="order-item">
						<my-card
							class="order_list--van-card"
							:idid="index"
							:title="item.orderItem.title"
							:quantity="item.quantity"
							:price="item.orderItem.price"
							:thumb="item.orderItem.image"
						/>

						<view class="order-footer-btns">
							<button size="mini" @click="toOrderDetail(item.order_id)">查看详情</button>
							<button size="mini" v-if="item.status == 0" @click="service_cancel(index)">撤销申请</button>
							<button size="mini" v-if="item.status == 1" @click="service_complete(index)">确认解决</button>
						</view>
					</view>

					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
import empty from '@/components/empty';
import myCard from '@/components/my-card/my-card.vue';

export default {
	components: {
		uniLoadMore,
		empty,
		myCard
	},
	data() {
		return {
			tabCurrentIndex: 0,
			navList: [
				{
					state: 0,
					page: 1,
					text: '待处理',
					loadingType: 'more',
					orderList: []
				},
				{
					state: 1,
					page: 1,
					text: '处理中',
					loadingType: 'more',
					orderList: []
				},
				{
					state: 10,
					page: 1,
					text: '处理完成',
					loadingType: 'more',
					orderList: []
				}
			]
		};
	},
	onLoad(options) {
		/**
		 * 修复app端点击除全部订单外的按钮进入时不加载数据的问题
		 * 替换onLoad下代码即可
		 */
		this.tabCurrentIndex = +options.state;
		// #ifndef MP
		this.loadData();
		// #endif
		// #ifdef MP
		if (options.state == 0) {
			this.loadData();
		}
		// #endif
	},
	methods: {
		
		
		
		// 已解决
		service_complete(index) {
			
			let that = this
			
			let serivce_id = that.navList[that.tabCurrentIndex].orderList[index].id
			
			uni.showModal({
				 title: '提示',
				    content: '问题是否解决了？',
				    success: function (res) {
				        if (res.confirm) {
							
							
							that.$http.post('/shop.order/service_complete', {
							  service_id: serivce_id
							}).then(res => {
								
							  if (res.data.data > 0) {
								  
								  
								  //that.navList[that.tabCurrentIndex].orderList[index].status = 10
								  
								  
								  uni.showToast({								  								 
								  	title:"操作成功"
								  })
								  
								  that.navList[that.tabCurrentIndex].orderList[index].loaded = true
								  that.loadData('tabChange');
								  
							     
							  } else {
							   // Toast.fail(res.msg);
							  }
							});
							
				            
							 
							
							
				        } else if (res.cancel) {
				            // console.log('用户点击取消');
				        }
				    }
			})
			
			
		   
		
		},
		
		
		// 撤消
		service_cancel(index) {
			
			
			
			let that = this
			
			let serivce_id = that.navList[that.tabCurrentIndex].orderList[index].id
			
			uni.showModal({
				 title: '提示',
				    content: '问题是否解决了？',
				    success: function (res) {
				        if (res.confirm) {
							
							
							that.$http.post('/shop.order/service_cancel', {
							  service_id: serivce_id
							}).then(res => {
								
							  if (res.data.data > 0) {
								  
								  
								//  
								  
								  uni.showToast({								  								 
								  	title:"撤销成功"
								  })
								  
								  that.navList[that.tabCurrentIndex].orderList[index].loaded = true
								  that.loadData('tabChange');
								  
							     
							  } else {
							   // Toast.fail(res.msg);
							  }
							});
							
				            
							 
							
							
				        } else if (res.cancel) {
				            // console.log('用户点击取消');
				        }
				    }
			})
			
			
			 
		},
		
		
		toOrderDetail(order_id){
			
			uni.navigateTo({
				url: './orderView?order_id=' + order_id
			})
			
		},
		
		
		loadData(source) {
			//这里是将订单挂载到tab列表下
			let index = this.tabCurrentIndex;
			let navItem = this.navList[index];

			console.log(index);

			let state = navItem.state;

			if (source === 'tabChange' && navItem.loaded === true) {
				//tab切换只有第一次需要加载数据
				return;
			}
			if (navItem.loadingType === 'loading') {
				//防止重复加载
				return;
			}

			navItem.loadingType = 'loading';

			this.$http
				.post('/shop.order/service', {
					page_size: 10,
					page: navItem.page,
					status: state
				})
				.then(res => {
					navItem.orderList.push(...(res.data.data || []));

					//
					this.$set(navItem, 'loaded', true);

					if (res.data.data.length === 0) {
						//判断是否还有数据， 有改为 more， 没有改为noMore
						navItem.loadingType = 'noMore';
					} else if (navItem.page === 1 && res.data.data.length < 10) {
						navItem.loadingType = 'noMore';
					} else {
						//判断是否还有数据， 有改为 more， 没有改为noMore
						navItem.loadingType = 'more';
					}

					// 下一页
					navItem.page++;
				});
		},

		name() {},

		//swiper 切换
		changeTab(e) {
			this.tabCurrentIndex = e.target.current;
			this.loadData('tabChange');
		},
		//顶部tab点击
		tabClick(index) {
			this.tabCurrentIndex = index;
		}
	}
};
</script>

<style lang="scss">
.order-item {
	background-color: white;
	margin-bottom: 20rpx;

	.order-footer-btns {
		padding-top: 20rpx;
		padding-bottom: 20rpx;
		text-align: right;
		button {
			margin-right: 20rpx;
		}
	}
}

.swiper-box {
	height: calc(100% - 40px);
}
.list-scroll-content {
	height: 100%;
}

.navbar {
	display: flex;
	height: 40px;
	padding: 0 5px;
	background: #fff;
	box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
	position: relative;
	z-index: 10;
	.nav-item {
		flex: 1;
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100%;
		font-size: 15px;
		color: #000000;
		position: relative;
		&.current {
			color: $colorPrimary;
			&:after {
				content: '';
				position: absolute;
				left: 50%;
				bottom: 0;
				transform: translateX(-50%);
				width: 44px;
				height: 0;
				border-bottom: 2px solid $colorPrimary;
			}
		}
	}
}

/* load-more */
.uni-load-more {
	display: flex;
	flex-direction: row;
	height: 80upx;
	align-items: center;
	justify-content: center;
}

.uni-load-more__text {
	font-size: 28upx;
	color: #999;
}

.uni-load-more__img {
	height: 24px;
	width: 24px;
	margin-right: 10px;
}

.uni-load-more__img > view {
	position: absolute;
}

.uni-load-more__img > view view {
	width: 6px;
	height: 2px;
	border-top-left-radius: 1px;
	border-bottom-left-radius: 1px;
	background: #999;
	position: absolute;
	opacity: 0.2;
	transform-origin: 50%;
	animation: load 1.56s ease infinite;
}

.uni-load-more__img > view view:nth-child(1) {
	transform: rotate(90deg);
	top: 2px;
	left: 9px;
}

.uni-load-more__img > view view:nth-child(2) {
	transform: rotate(180deg);
	top: 11px;
	right: 0;
}

.uni-load-more__img > view view:nth-child(3) {
	transform: rotate(270deg);
	bottom: 2px;
	left: 9px;
}

.uni-load-more__img > view view:nth-child(4) {
	top: 11px;
	left: 0;
}
</style>
